package com.zjdiepu.www.partner.api.hzicbc;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.diepu.www.client.proxy.hzicbc.request.CreditResultRequest;
import com.diepu.www.client.proxy.hzicbc.request.ObtainFileList;
import com.diepu.www.client.proxy.hzicbc.request.ObtainFileListRequest;
import com.diepu.www.client.proxy.partner.common.ObtainFileListResponse;
import com.diepu.www.client.proxy.partner.request.own.DiepuIcbcQryMarginRequest;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.ibiz.zpp.IcbcApiService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.icbc.dto.OtherLoanLossProvision;
import com.zjdiepu.www.response.BaseResponse;
import com.zjdiepu.www.response.BooleanResponse;
import com.zjdiepu.www.security.server.convert.MyJson;
import com.zjdiepu.www.security.server.core.Partners;
import com.zjdiepu.www.security.server.interceptor.PartnerProvider;

@MyJson
@RestController
@RequestMapping(value="IcbcApiController", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@PartnerProvider(partner = Partners.HZ_ICBC)
public class IcbcApiController {

	@Resource
	private IcbcApiService icbcApiService;
	
	/**
	 * 任务描述
	 * 对应表【other_loan_loss_provision】
	 * 杭州工行对应的银行编码是【ICBC】，接口接收到信息后更新【other_loan_loss_provision】，如果杭州工行原先已经存在数据，将原先数据状态设置为 dp_status = 3；写入新的信息；
	 * @param diepuIcbcQryMarginRequest
	 * @return
	 */
	@RequestMapping(value="diepuIcbcQryMargin",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public BaseResponse<BooleanResponse> diepuIcbcQryMargin(@Validated @RequestBody DiepuIcbcQryMarginRequest diepuIcbcQryMarginRequest)
	{
		if(null == diepuIcbcQryMarginRequest)
		{
			throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		}
		SysUser sysUser = new SysUser();
		sysUser.setId(diepuIcbcQryMarginRequest.getUserId());
		sysUser.setUname(diepuIcbcQryMarginRequest.getLoginName());
		
		if(null == diepuIcbcQryMarginRequest.getAccounts() || diepuIcbcQryMarginRequest.getAccounts().size() == 0)
		{
			throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		}
		
		for(int i=0;i<diepuIcbcQryMarginRequest.getAccounts().size();i++)
		{
			OtherLoanLossProvision otherLoanLossProvision = new OtherLoanLossProvision();
			BeanUtils.copyProperties(diepuIcbcQryMarginRequest.getAccounts().get(i), otherLoanLossProvision);
			otherLoanLossProvision.setSubAccno(diepuIcbcQryMarginRequest.getAccounts().get(i).getSub());
			icbcApiService.diepuUpdateRiskReserve(sysUser, otherLoanLossProvision);
		}
		return BaseResponse.successWithBoolean(true);
	}
	
	
	/**
	 *  新增接口：接口查征信结果反馈
	 * @param diepuIcbcQryMarginRequest
	 * @return
	 */
	@RequestMapping(value="diepuCreditResult",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public BaseResponse<BooleanResponse> diepuCreditResult(@Validated @RequestBody CreditResultRequest creditResultRequest)throws Exception
	{
//		@update 2018-03-01 modifier_name为：杭州工行；modifier为 32个0；（3.1新增）
		SysUser sysUser = new SysUser();
		sysUser.setId(SystemConsts.DEFAULT_ID);
		sysUser.setUname("接口查征信结果反馈 ");
		icbcApiService.diepuCreditResult(sysUser, creditResultRequest);
		return BaseResponse.successWithBoolean(true);
	}
	
	/**
	 * 只有的 从主程序获取附件列表 的定时任务
	 * @param obtainFileListRequest
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="obtainFileList",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public BaseResponse<ObtainFileListResponse> obtainFileList(@Validated @RequestBody ObtainFileListRequest obtainFileListRequest)throws Exception
	{
		SysUser sysUser = new SysUser();
		sysUser.setId("00000000000000000000000000000000");
		sysUser.setUname("主程序获取附件列表");
		List<ObtainFileList> obtainFileLists = icbcApiService.diepuObtainFileList(sysUser, obtainFileListRequest);
		ObtainFileListResponse response = new ObtainFileListResponse();
		response.setObtainFileLists(obtainFileLists);
		return BaseResponse.success(response);
	}
}
